package cn.jiyun.controller;

import cn.jiyun.pojo.User;
import cn.jiyun.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //查询所有
    @GetMapping("/findAll")
    public String findAll(@RequestParam(required = true,defaultValue = "1")Integer pageNum, Model model){
        PageHelper.startPage(pageNum,2);
        List<User> list = userService.findAll();
        PageInfo<User> page = new PageInfo<>(list);
        model.addAttribute("page",page);
        return "list";
    }
    //删除
    @GetMapping("/del")
    public String del(Integer uid){
        userService.del(uid);
        return "redirect:findAll";
    }
    //查询id
    @GetMapping("/findByUid")
    public String findByEid(Model model,Integer uid){
        User user=userService.findByUid(uid);
        model.addAttribute("user",user);
        return "update";
    }
    //修改
    @PostMapping("/update")
    public String update(User user){
        userService.update(user);
        return "redirect:findAll";
    }
    //添加
    @PostMapping("/add")
    public String add(User user){
        userService.add(user);
        return "redirect:findAll";
    }
    //跳转
    @GetMapping("/toAdd")
    public String toAdd(){
        return "add";
    }
}
